<template>
  <div>
    <van-nav-bar title="编辑资料" @click-left="$router.go(-1)" left-arrow />
    <div class="avatar">
      <van-image
        width="120px"
        height="120px"
        fit="cover"
        round
        :src="userInfo.photo"
      />
      <van-uploader :after-read="afterRead" />
    </div>
    <van-cell-group>
      <van-cell title="名称" is-link :value="userInfo.name" @click="editName" />
      <van-cell
        title="性别"
        is-link
        :value="userInfo.gender ? '女' : '男'"
        @click="showPopup = true"
      />
      <van-cell
        title="生日"
        is-link
        :value="userInfo.birthday"
        @click="showBirthday = true"
      />
    </van-cell-group>
    <van-dialog
      v-model="showDialog"
      title="修改昵称"
      show-cancel-button
      @confirm="editUserName"
    >
      <van-field v-model="newName" ref="name" placeholder="请输入用户名" />
    </van-dialog>
    <!-- 性别弹出框 -->
    <van-popup v-model="showPopup" position="bottom">
      <van-nav-bar
        title="修改性别"
        left-text="取消"
        @click-left="showPopup = false"
      />
      <van-cell title="男" is-link @click="editdateGender(0)" />
      <van-cell title="女" is-link @click="editdateGender(1)" />
    </van-popup>
    <!-- 日期弹窗 -->
    <van-popup v-model="showBirthday" position="bottom">
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
        @cancel="showBirthday = false"
        @confirm="editUserBirthday"
      />
    </van-popup>
    <div class="cropper-box" v-show="showCropper">
      <vueCropper
        ref="cropper"
        :img="img"
        autoCrop
        autoCropWidth="120px"
        autoCropHeight="120px"
      ></vueCropper>
      <van-button type="primary" @click="editUserCropper">确认</van-button>
      <van-button type="info" @click="showCropper = false">取消</van-button>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import { edituserInfo } from "./user.js";
import moment from "moment";
import { edituserPhoto } from "./user.js";
import { VueCropper } from "vue-cropper";

export default {
  name: "edit",
  data() {
    return {
      showDialog: false,
      newName: "",
      showPopup: false,
      showBirthday: false,
      minDate: new Date(1980, 0, 1),
      maxDate: new Date(2025, 11, 31),
      currentDate: new Date(2021, 0, 17),
      showCropper: false,
      img: ""
    };
  },
  components: {
    VueCropper
  },
  methods: {
    editName() {
      this.showDialog = true;
      this.newName = this.userInfo.name;
      // this.$nextTick  Vue自带的一个高级定时器
      this.$nextTick(() => {
        this.$refs.name.focus();
      });
    },
    async editUserName() {
      let res = await edituserInfo({ name: this.newName });
      //(1)弹窗提示
      this.$toast.success("更新成功!");
      //(2)更新vuex数据
      this.$store.commit("update", {
        ...this.userInfo, //先传入原始数据
        name: this.newName //然后使用新的name覆盖原始的name
      });
    },
    // 修改性别
    async editdateGender(gender) {
      let res = await edituserInfo({ gender });

      // console.log(res);
      this.$toast.success("修改成功");
      this.$store.commit("update", {
        ...this.userInfo,
        gender: res.data.data.gender == 1 ? 1 : 0
      });
      // 修改完成隐藏弹框
      this.showPopup = false;
    },
    editUserBirthday(value) {
      let birthday = moment(value).format("YYYY-MM-DD");
      edituserInfo({ birthday }).then(res => {
        this.$toast.success("更新成功");
        this.$store.commit("update", {
          ...this.userInfo,
          birthday: res.data.data.birthday
        });
        this.showBirthday = false;
      });
    },
    afterRead(file) {
      // 此时可以自行将文件上传至服务器
      console.log(file);
      this.img = file.content;
      this.showCropper = true;
    },
    editUserCropper() {
      this.$refs.cropper.getCropBlob(blod => {
        let fd = new FormData();
        fd.append("photo", blod);
        edituserPhoto(fd).then(res => {
          console.log(res);
          this.$toast.success("更新成功");
          this.$store.commit("update", {
            ...this.userInfo,
            photo: res.data.data.photo
          });
          this.showCropper = false;
        });
      });
    }
  },

  computed: {
    ...mapState(["userInfo"])
  }
};
</script>

<style lang="less">
.avatar {
  display: flex;
  justify-content: center;
  padding: 20px 0;
  .van-uploader__upload {
    width: 120px;
    height: 120px;
    border-radius: 50%;
    position: absolute;
    transform: translateX(-50%);
    margin-left: -60px;
    opacity: 0;
  }
}
.cropper-box {
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  .van-button--primary {
    position: absolute;
    left: 0;
    bottom: 0;
  }
  .van-button--info {
    position: absolute;
    right: 0;
    bottom: 0;
  }
}
</style>
